package com.snail.monitor;

import org.apache.commons.lang3.StringUtils;

import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.Map;

public class OperateRecordInfo {

    public static final String OP_TYPE_KEYBOARD = "keyboard";

    public static final String OP_TYPE_MOUSE = "mouse";

    public static final String OP_TYPE_DELAY = "delay";

    public static final String OP_CODE_KEY_DOWN = "keydown";

    public static final String OP_CODE_KEY_UP = "keyup";

    public static final String OP_CODE_MOUSE_LEFT_CLICK = "lclick";

    public static final String OP_CODE_MOUSE_RIGHT_CLICK = "rclick";

    private static Map<String, String> opTypeAndCodeName = new HashMap<String, String>();

    static {
        opTypeAndCodeName.put("", "");
        opTypeAndCodeName.put(null, "");
        opTypeAndCodeName.put(OP_TYPE_KEYBOARD, "");
        opTypeAndCodeName.put(OP_TYPE_MOUSE, "");
        opTypeAndCodeName.put(OP_TYPE_DELAY, "等待");
        opTypeAndCodeName.put(OP_CODE_KEY_DOWN, "按下");
        opTypeAndCodeName.put(OP_CODE_KEY_UP, "释放");
        opTypeAndCodeName.put(OP_CODE_MOUSE_LEFT_CLICK, "左击");
        opTypeAndCodeName.put(OP_CODE_MOUSE_RIGHT_CLICK, "右击");
    }

    private String opType;

    private String opCode;

    private String opValue;

    private OperateRecordInfo(String opType, String opCode, String opValue) {
        super();
        this.opType = opType;
        this.opCode = opCode;
        this.opValue = opValue;
    }

    public String getFormat() {
        return getOpType() + " " + getOpCode() + " " + getOpValue();
    }

    public String toString() {
        String opTypeName = (String) opTypeAndCodeName.get(this.opType);
        String opCodeName = (String) opTypeAndCodeName.get(this.opCode);
        String opValueName = "";
        if (OP_TYPE_KEYBOARD.equals(this.opType)) {
            opValueName = KeyEvent.getKeyText(Integer.parseInt(this.opValue)) + "键";
        } else if (OP_TYPE_DELAY.equals(this.opType)) {
            opValueName = this.opValue + "ms";
        } else {
            opValueName = this.opValue;
        }
        return opTypeName + opCodeName + opValueName;
    }

    public static OperateRecordInfo newOperateRecordInfo(String opRecStr) {
        if (StringUtils.isBlank(opRecStr)) {
            return null;
        }
        String[] strArray = opRecStr.split(" ");
        if (strArray.length != 3) {
            return null;
        }
        return new OperateRecordInfo(strArray[0], strArray[1], strArray[2]);
    }

    public static OperateRecordInfo newOperateRecordInfo(String opType, String opCode, String opValue) {
        return new OperateRecordInfo(opType, opCode, opValue);
    }

    public String getOpType() {
        return opType;
    }

    public void setOpType(String opType) {
        this.opType = opType;
    }

    public String getOpCode() {
        return opCode;
    }

    public void setOpCode(String opCode) {
        this.opCode = opCode;
    }

    public String getOpValue() {
        return opValue;
    }

    public void setOpValue(String opValue) {
        this.opValue = opValue;
    }

}
